function varargout = GUI(varargin)
%GUI M-file for GUI.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('Property','Value',...) creates a new GUI using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to GUI_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      GUI('CALLBACK') and GUI('CALLBACK',hObject,...) call the
%      local function named CALLBACK in GUI.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help GUI

% Last Modified by GUIDE v2.5 11-Oct-2013 02:14:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
global NumRun;
NumRun = 0;
varargout{1} = handles.output;



function pathImage_Callback(hObject, eventdata, handles)
% hObject    handle to pathImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of pathImage as text
%        str2double(get(hObject,'String')) returns contents of pathImage as a double


% --- Executes during object creation, after setting all properties.
function pathImage_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pathImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in btnRun.
function btnRun_Callback(hObject, eventdata, handles)
clear listImage;
clear listCharacter;
global listImage;
global listCharacter;
global NumRun;
global isPushCheck;
global conSauvola;
global conFilter1;
global conFilter2;
isPushCheck = 0;
NumRun= NumRun + 1;

global listAllFileRun;
global currentImageRun;
% hObject    handle to btnRun (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
IS_DEBUG = false;
IS_GAUSS = false;
IS_RUN_FOLDER = get(handles.cbIsRunFolder,'value');
FOLDER_RESULT = get(handles.txtNameFolderResult,'string');
if exist(FOLDER_RESULT,'dir') == 0
        mkdir(FOLDER_RESULT);
end

% get Sauvola config
conSauvola_k = str2num(get(handles.txtSauvolaConfigK,'string'));
conSauvola_R = str2num(get(handles.txtSauvolaConfigR,'string'));
conSauvola_b = str2num(get(handles.txtSauvolaConfigB,'string'));
conSauvola = [conSauvola_k conSauvola_R conSauvola_b];

% get Filter 1 config
conFilter1_Aspect = str2num(get(handles.txtFilter1Aspect,'string'));
conFilter1_Orientation = str2num(get(handles.txtFilter1Orientation,'string'));
conFilter1_Euler = str2num(get(handles.txtFilter1Euler,'string'));
conFilter1 = [conFilter1_Aspect conFilter1_Orientation conFilter1_Euler];

%get Filter 2 config
conFilter2_height = str2num(get(handles.txtFilter2Height,'string'));
conFilter2_width = str2num(get(handles.txtFilter2Width,'string'));
conFilter2_nLeast = str2num(get(handles.txtFilter2NumLeast,'string'));
conFilter2_nRecheck = str2num(get(handles.txtFilter2NumRecheck,'string'));
conFilter2 = [conFilter2_height conFilter2_width conFilter2_nLeast conFilter2_nRecheck];


h=waitbar(0, 'Loading...');
    waitbar(5/100);
if (IS_RUN_FOLDER == 0)
    filename = get(handles.pathImage,'string');
    [pathstr, name, ext] = fileparts(filename);
    Io = imread(filename);  % nhap buc anh goc va gan vao I1
    
    fid = fopen(strcat(get(handles.txtNameFolderResult,'string'),'\','logResult.txt'), 'at');
    %write to file name of file
    fprintf(fid, ' %s ', filename);
    
    [x y z] = size(Io);
    if z==3
        Io = rgb2gray(Io); 
    end
    axes(handles.axeImageOgirinal);
    imshow(Io);
    [time timeSCW timeSauvola timeMeasure lsImage lsCharacter] = main( Io, true, ...
        IS_GAUSS, filename, FOLDER_RESULT, conSauvola, conFilter1, conFilter2 );
    listImage = lsImage;
    listCharacter = lsCharacter;
    %{
    set(handles.txtTimeRun,'String', [num2str(time(5)) ' phut  ' ...
        num2str(time(6)) ' giay']);
    set(handles.txtTimeSauvola,'String', [num2str(timeSauvola(5)) ' phut  ' ...
        num2str(timeSauvola(6)) ' giay']);
    set(handles.txtTimeSCW,'String', [num2str(timeSCW(5)) ' phut  ' ...
        num2str(timeSCW(6)) ' giay']);
    set(handles.txtTimeMeasure,'String', [num2str(timeMeasure(5)) ' phut  ' ...
        num2str(timeMeasure(6)) ' giay']);
    
    %}
    set(handles.txtNumLP,'String', num2str(length(lsImage)));
    if (length(lsImage)) > 0
        axes(handles.axeImageResult);
        imshow(lsImage{1});
        set(handles.txtCurrent,'String', '1');
        set(handles.txtResultCharacter,'String',listCharacter{1});
    else
        axes(handles.axeImageResult);
        imshow(0);
        set(handles.txtCurrent,'String', '0');
    end 

    
    %__ write result to file bmp
    for j=1:length(listImage)        
        imwrite(listImage{j},strcat(get(handles.txtNameFolderResult,'string'),'\', ...
                name,'re',num2str(j),ext),'bmp');
    end
    %____________
    
    %__ write to file
    fprintf(fid, ' %d \n', length(listImage));
    % dong file text lai
    fclose(fid);
    clear fid;
    %__________
else
    % run with mode runfolder 
    % if IS_DEBUG , run a image each time, control via button
    
    allFiles = dir(get(handles.pathImage,'string'));
    filename = { allFiles.name };

    if (IS_DEBUG == 1)  %IS_DEBUG
        listAllFileRun = filename;
        currentImageRun = 2;
    else
        for i=3:length(filename)
            chec = 0;
            if (strfind(filename{i},'.JPG')>0) 
                chec = 1;
            end
            if (strfind(filename{i},'.jpg')>0) 
                chec = 1;
            end
            if (strfind(filename{i},'.bmp')>0) 
                chec = 1;
            end
            if (chec) 
                [pathstr, name, ext] = fileparts(filename{i});
                fid = fopen(strcat(get(handles.txtNameFolderResult,'string'),'\','logResult.txt'), 'at');
                %write to file name of file
                fprintf(fid, '%d %s ', i, filename{i});

                Io = imread(strcat(get(handles.pathImage,'string'),'\',filename{i}));  % nhap buc anh goc va gan vao I1
                [x y z] = size(Io);
                if z==3
                    Io = rgb2gray(Io); 
                end            

                [time timeSCW timeSauvola timeMeasure lsImage lsCharacter ] = main( Io, true, ...
            IS_GAUSS, filename{i}, FOLDER_RESULT,  conSauvola, conFilter1, conFilter2 );
                listImage = lsImage;
                listCharacter = lsCharacter;
                for j=1:length(listImage)
                imwrite(listImage{j},strcat(get(handles.txtNameFolderResult,'string'),'\', ...
                    name,'refinal',num2str(j),'char-',listCharacter{j},ext),'bmp');
                end

                %write to file name of file
                fprintf(fid, ' %d \n', length(listImage));
                % dong file text lai
                fclose(fid);
                clear fid;
            end
        end
    end

    
end

waitbar(100/100);
close(h);


% --- Executes on button press in NextResult.
function NextResult_Callback(hObject, eventdata, handles)
% hObject    handle to NextResult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global listImage;
global listCharacter;
currentIn = str2num(get(handles.txtCurrent, 'String')) ;
maxImage = length(listImage);
if currentIn + 1 <= maxImage
    set(handles.txtCurrent,'String', num2str(currentIn + 1 ));
    set(handles.txtResultCharacter,'String',listCharacter{currentIn + 1});
    axes(handles.axeImageResult);    
    imshow(listImage{currentIn + 1});
end



% --- Executes on button press in PreResult.
function PreResult_Callback(hObject, eventdata, handles)
% hObject    handle to PreResult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global listImage;
global listCharacter;
currentIn = str2num(get(handles.txtCurrent, 'String')) ;
maxImage = length(listImage);
if currentIn - 1 >= 1
    set(handles.txtCurrent,'String', num2str(currentIn - 1 ));
    set(handles.txtResultCharacter,'String',listCharacter{currentIn - 1});
    axes(handles.axeImageResult);
    imshow(listImage{currentIn - 1});
end


% --- Executes on button press in btnBrowse.
function btnBrowse_Callback(hObject, eventdata, handles)
% hObject    handle to btnBrowse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if (get(handles.cbIsRunFolder,'value'))
set(handles.pathImage,'String', uigetdir('*.*', 'Choose folder contain data'));
else
    [FileName PathName]= uigetfile('*.*','browse');
    set(handles.pathImage,'String', [PathName FileName]);
end
% set(handles.pathImage,'String', uigetdir('*.*', 'Choose folder contain data'));
% --- Executes on button press in btnCheckRight.





% --- Executes on button press in cbEnableDebug.
function cbEnableDebug_Callback(hObject, eventdata, handles)
% hObject    handle to cbEnableDebug (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cbEnableDebug
if (get(hObject,'value') == 0 ) 
        set(handles.btnNextImage,'visible','off');
        set(handles.btnPreImage,'visible','off');
        set(handles.txtNameImageCurrent,'visible','off');
else
    set(handles.btnNextImage,'visible','on');
    set(handles.btnPreImage,'visible','on');
    set(handles.txtNameImageCurrent,'visible','on');
end

% --- Executes on button press in cbEnableGauss.
function cbEnableGauss_Callback(hObject, eventdata, handles)
% hObject    handle to cbEnableGauss (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cbEnableGauss


% --- Executes on button press in cbIsRunFolder.
function cbIsRunFolder_Callback(hObject, eventdata, handles)
% hObject    handle to cbIsRunFolder (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cbIsRunFolder



function txtNameFolderResult_Callback(hObject, eventdata, handles)
% hObject    handle to txtNameFolderResult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtNameFolderResult as text
%        str2double(get(hObject,'String')) returns contents of txtNameFolderResult as a double


% --- Executes during object creation, after setting all properties.
function txtNameFolderResult_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtNameFolderResult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in btnBrowseResult.
function btnBrowseResult_Callback(hObject, eventdata, handles)
% hObject    handle to btnBrowseResult (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.txtNameFolderResult,'String', uigetdir('C:\', 'Choose empty folder to save result'));



function txtSCWConfigX1_Callback(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigX1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSCWConfigX1 as text
%        str2double(get(hObject,'String')) returns contents of txtSCWConfigX1 as a double


% --- Executes during object creation, after setting all properties.
function txtSCWConfigX1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigX1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtSCWConfigY1_Callback(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigY1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSCWConfigY1 as text
%        str2double(get(hObject,'String')) returns contents of txtSCWConfigY1 as a double


% --- Executes during object creation, after setting all properties.
function txtSCWConfigY1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigY1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cbIsUseMean.
function cbIsUseMean_Callback(hObject, eventdata, handles)
% hObject    handle to cbIsUseMean (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cbIsUseMean



function txtSCWConfigX2_Callback(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigX2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSCWConfigX2 as text
%        str2double(get(hObject,'String')) returns contents of txtSCWConfigX2 as a double


% --- Executes during object creation, after setting all properties.
function txtSCWConfigX2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigX2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtSCWConfigY2_Callback(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigY2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSCWConfigY2 as text
%        str2double(get(hObject,'String')) returns contents of txtSCWConfigY2 as a double


% --- Executes during object creation, after setting all properties.
function txtSCWConfigY2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigY2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtSCWConfigT_Callback(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSCWConfigT as text
%        str2double(get(hObject,'String')) returns contents of txtSCWConfigT as a double


% --- Executes during object creation, after setting all properties.
function txtSCWConfigT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSCWConfigT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtSauvolaConfigK_Callback(hObject, eventdata, handles)
% hObject    handle to txtSauvolaConfigK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSauvolaConfigK as text
%        str2double(get(hObject,'String')) returns contents of txtSauvolaConfigK as a double


% --- Executes during object creation, after setting all properties.
function txtSauvolaConfigK_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSauvolaConfigK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtSauvolaConfigR_Callback(hObject, eventdata, handles)
% hObject    handle to txtSauvolaConfigR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSauvolaConfigR as text
%        str2double(get(hObject,'String')) returns contents of txtSauvolaConfigR as a double


% --- Executes during object creation, after setting all properties.
function txtSauvolaConfigR_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSauvolaConfigR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtSauvolaConfigB_Callback(hObject, eventdata, handles)
% hObject    handle to txtSauvolaConfigB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtSauvolaConfigB as text
%        str2double(get(hObject,'String')) returns contents of txtSauvolaConfigB as a double


% --- Executes during object creation, after setting all properties.
function txtSauvolaConfigB_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtSauvolaConfigB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter1Aspect_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter1Aspect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter1Aspect as text
%        str2double(get(hObject,'String')) returns contents of txtFilter1Aspect as a double


% --- Executes during object creation, after setting all properties.
function txtFilter1Aspect_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter1Aspect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter1Orientation_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter1Orientation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter1Orientation as text
%        str2double(get(hObject,'String')) returns contents of txtFilter1Orientation as a double


% --- Executes during object creation, after setting all properties.
function txtFilter1Orientation_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter1Orientation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter1Euler_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter1Euler (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter1Euler as text
%        str2double(get(hObject,'String')) returns contents of txtFilter1Euler as a double


% --- Executes during object creation, after setting all properties.
function txtFilter1Euler_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter1Euler (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter2Height_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter2Height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter2Height as text
%        str2double(get(hObject,'String')) returns contents of txtFilter2Height as a double


% --- Executes during object creation, after setting all properties.
function txtFilter2Height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter2Height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter2Width_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter2Width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter2Width as text
%        str2double(get(hObject,'String')) returns contents of txtFilter2Width as a double


% --- Executes during object creation, after setting all properties.
function txtFilter2Width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter2Width (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter2NumLeast_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter2NumLeast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter2NumLeast as text
%        str2double(get(hObject,'String')) returns contents of txtFilter2NumLeast as a double


% --- Executes during object creation, after setting all properties.
function txtFilter2NumLeast_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter2NumLeast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtFilter2NumRecheck_Callback(hObject, eventdata, handles)
% hObject    handle to txtFilter2NumRecheck (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFilter2NumRecheck as text
%        str2double(get(hObject,'String')) returns contents of txtFilter2NumRecheck as a double


% --- Executes during object creation, after setting all properties.
function txtFilter2NumRecheck_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFilter2NumRecheck (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in btnNextImage.
function btnNextImage_Callback(hObject, eventdata, handles)
% hObject    handle to btnNextImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global listAllFileRun;
global currentImageRun;
global conSauvola;
global conFilter1;
global conFilter2;
global listImage;
global listCharacter;
currentImageRun = currentImageRun + 1;
IS_DEBUG = get(handles.cbEnableDebug,'value');
IS_GAUSS = false;
IS_RUN_FOLDER = get(handles.cbIsRunFolder,'value');
FOLDER_RESULT = get(handles.txtNameFolderResult,'string');
chec = 0;
if (currentImageRun <= length(listAllFileRun))
    
    fileNameCurrent = listAllFileRun{currentImageRun};
    [pathstr, name, ext] = fileparts(fileNameCurrent);
    h=waitbar(0, strcat('Running - ',name));
    waitbar(5/100);
    set(handles.txtNameImageCurrent,'string',name);
    if (strfind(fileNameCurrent,'.JPG')>0)
        chec = 1;
    end
    if (strfind(fileNameCurrent,'.jpg')>0)
        chec = 1;
    end
    if (strfind(fileNameCurrent,'.bmp')>0)
        chec = 1;
    end
    if (chec)
%         [pathstr, name, ext] = fileparts(fileNameCurrent);
       
        fid = fopen(strcat(get(handles.txtNameFolderResult,'string'),'\','logResult.txt'), 'at');
        %write to file name of file
        fprintf(fid, '%d %s ', currentImageRun, fileNameCurrent);
        
        Io = imread(strcat(get(handles.pathImage,'string'),'\',fileNameCurrent));  % nhap buc anh goc va gan vao I1
        [x y z] = size(Io);
        if z==3
            Io = rgb2gray(Io);
        end
        axes(handles.axeImageOgirinal);
        imshow(Io);
        [time timeSCW timeSauvola timeMeasure lsImage lsCharacter ] = main( Io, IS_DEBUG, ...
            IS_GAUSS, fileNameCurrent, FOLDER_RESULT,  conSauvola, conFilter1, conFilter2 );
        listImage = lsImage;
        listCharacter = lsCharacter;
        
        set(handles.txtNumLP,'String', num2str(length(lsImage)));
        if (length(lsImage)) > 0
            axes(handles.axeImageResult);
            imshow(lsImage{1});
            set(handles.txtCurrent,'String', '1');
            set(handles.txtResultCharacter,'String',listCharacter{1});
        else
            axes(handles.axeImageResult);
            imshow(0);
            set(handles.txtCurrent,'String', '0');
        end
        
        for j=1:length(listImage)
            imwrite(listImage{j},strcat(get(handles.txtNameFolderResult,'string'),'\', ...
                name,'refinal',num2str(j),'char-',listCharacter{j},ext),'bmp');
        end
        
        %write to file name of file
        fprintf(fid, ' %d \n', length(listImage));
        % dong file text lai
        fclose(fid);
        clear fid;
    end
    waitbar(100/100);
    close(h);
end

% --- Executes on button press in btnPreImage.
function btnPreImage_Callback(hObject, eventdata, handles)
% hObject    handle to btnPreImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global listAllFileRun;
global currentImageRun;
global conSauvola;
global conFilter1;
global conFilter2;
global listImage;
global listCharacter;
currentImageRun = currentImageRun - 1;
IS_DEBUG = get(handles.cbEnableDebug,'value');
IS_GAUSS = false;
IS_RUN_FOLDER = get(handles.cbIsRunFolder,'value');
FOLDER_RESULT = get(handles.txtNameFolderResult,'string');
chec = 0;
if (currentImageRun <= length(listAllFileRun) && currentImageRun >1)
    
    fileNameCurrent = listAllFileRun{currentImageRun};
    [pathstr, name, ext] = fileparts(fileNameCurrent);
    h=waitbar(0, strcat('Running - ',name));
    waitbar(5/100);
    set(handles.txtNameImageCurrent,'string',name);
    if (strfind(fileNameCurrent,'.JPG')>0)
        chec = 1;
    end
    if (strfind(fileNameCurrent,'.jpg')>0)
        chec = 1;
    end
    if (strfind(fileNameCurrent,'.bmp')>0)
        chec = 1;
    end
    if (chec)
%         [pathstr, name, ext] = fileparts(fileNameCurrent);
       
        fid = fopen(strcat(get(handles.txtNameFolderResult,'string'),'\','logResult.txt'), 'at');
        %write to file name of file
        fprintf(fid, '%d %s ', currentImageRun, fileNameCurrent);
        
        Io = imread(strcat(get(handles.pathImage,'string'),'\',fileNameCurrent));  % nhap buc anh goc va gan vao I1
        [x y z] = size(Io);
        if z==3
            Io = rgb2gray(Io);
        end
        axes(handles.axeImageOgirinal);
        imshow(Io);
        [time timeSCW timeSauvola timeMeasure lsImage lsCharacter ] = main( Io, IS_DEBUG, ...
            IS_GAUSS, fileNameCurrent, FOLDER_RESULT,  conSauvola, conFilter1, conFilter2 );
        listImage = lsImage;
        listCharacter = lsCharacter;
        
        set(handles.txtNumLP,'String', num2str(length(lsImage)));
        if (length(lsImage)) > 0
            axes(handles.axeImageResult);
            imshow(lsImage{1});
            set(handles.txtCurrent,'String', '1');
            set(handles.txtResultCharacter,'String',listCharacter{1});
        else
            axes(handles.axeImageResult);
            imshow(0);
            set(handles.txtCurrent,'String', '0');
        end
        
        for j=1:length(listImage)
            imwrite(listImage{j},strcat(get(handles.txtNameFolderResult,'string'),'\', ...
                name,'refinal',num2str(j),'char-',listCharacter{j},ext),'bmp');
        end
        
        %write to file name of file
        fprintf(fid, ' %d \n', length(listImage));
        % dong file text lai
        fclose(fid);
        clear fid;
    end
    waitbar(100/100);
    close(h);
end
